<template>
	<view class="login">
		<view class="content">
			<view class="title">登录江湖百业</view>
			<!-- <view class="tabs">
				<view class="tabs_item" :class="type==1?'active':''" @click="type=1">账号登录</view>
				<view class="tabs_item" :class="type==2?'active':''" @click="type=2">验证码登录</view>
			</view> -->
			<view class="item">
				<view class="key">账号</view>
				<input type="text" placeholder="请输入账号" v-model="phone" class="input">
			</view>
			<view class="item" v-if="type==1">
				<view class="key">密码</view>
				<input type="text" password placeholder="请输入密码" v-model="password" class="input">
			</view>
			<view class="item" v-else>
				<view class="key">验证码</view>
				<view class="input_code">
					<input type="text" placeholder="请输入验证码" autocomplete="off" v-model="code">
					<view class="get_code" v-if="isTimer">{{time}}s</view>
					<view class="get_code" v-else @click="getCode">获取验证码</view>
				</view>
			</view>
			<view class="other">
				<view @click="router('/pages/login/find_password')">忘记密码</view>
				<view @click="router('/pages/login/register')">注册账号</view>
			</view>
			<view class="explain" @click="checked=!checked">
				<image :src="`${baseImageUrl}/common/checked.png`" v-if="checked"></image>
				<image :src="`${baseImageUrl}/common/check_no.png`" v-else></image>
				<view class="tips">
					<text>阅读并同意</text>
					<text class="desc" @click.stop="router('/pages/setting/article?key=agreement')">《用户协议》</text>
					<text>和</text>
					<text class="desc" @click.stop="router('/pages/setting/article?key=privacy')">《隐私协议》</text>
				</view>
			</view>
			<view class="btn" @click="submit">登录</view>
		</view>
	</view>
</template>

<script>
	import {
		smsSend,
		login,
		loginCode
	} from '@/request/api.js';
	import {
		captchaAppId
	} from "@/utils/config";
	export default {
		data() {
			return {
				phone: "",
				password: "",
				code: "",
				
				checked: false,
				type: 2
			}
		},
		methods: {
			getCode() {
				if (!this.phone) {
					this.$u.toast("请输入手机号");
					return false;
				};
				const captcha = new TencentCaptcha(captchaAppId, async (res) => {
					if (res.ret !== 0) return false;
					let {
						randstr,
						ticket
					} = res;
					this.isTimer = true;
					this.timer = setInterval(() => {
						this.time--
						if (this.time < 0) {
							this.isTimer = false;
							this.time = 60;
							clearInterval(this.timer);
						};
					}, 1000);
					await smsSend({
						phone: this.phone,
						randstr: randstr,
						ticket: ticket,
						scene: "login"
					});
					this.$u.toast("验证码已发送,请查看");
				}, {});
				captcha.show();
			},
			async submit() {
				if (!this.checked) {
					this.$u.toast("请先阅读《协议》");
					return false;
				}
				var res;
				if (this.type == 1) {
					res = await login({
						phone: this.phone,
						password: this.password
					});
				} else {
					res = await loginCode({
						phone: this.phone,
						code: this.code
					});
				}
				let user = res.data;
				let token = res.token;
				this.$store.commit('changeInfo', user);
				this.$store.commit('changeToken', token);
				this.$u.toast("登录成功");
				let role_id = user.role_id;
				setTimeout(() => {
					if (role_id == 0) {
						this.router("/pages/login/role");
					} else {
						this.redirectRouter(3);
					};
				}, 1000);
			}
		}
	}
</script>

<style scoped lang="scss">
	.login {
		width: 100%;
		height: 1624rpx;
		padding-top: 713rpx;
		padding-bottom: 60rpx;
		box-sizing: border-box;
		background: url(#{$baseImageUrl}/login/bg.png) no-repeat;
		background-size: 100% 100%;

		.content {
			width: 688rpx;
			height: 738rpx;
			margin: 0 auto;
			padding: 55rpx;
			box-sizing: border-box;
			background: url(#{$baseImageUrl}/bg/688_738.png) no-repeat;
			background-size: 100% 100%;
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			align-items: center;

			.title {
				color: #494338;
				font-size: 50rpx;
				font-weight: 400;
				font-family: LiSu;
			}

			.tabs {
				display: flex;
				justify-content: center;

				.tabs_item {
					width: 210rpx;
					height: 56rpx;
					line-height: 56rpx;
					text-align: center;
					color: #494338;
					font-size: 32rpx;
					font-weight: 400;
					margin: 0 20rpx;

					&.active {
						background: url(#{$baseImageUrl}/common/label.png) no-repeat;
						background-size: 100% 100%;
					}
				}
			}

			.item {
				width: 100%;

				.key {
					color: #494338;
					font-size: 32rpx;
					font-weight: 400;
					font-family: SimSun;
					margin-bottom: 20rpx;
				}

				.input {
					width: 100%;
					height: 80rpx;
					border-radius: 10rpx;
					padding-left: 45rpx;
					box-sizing: border-box;
					box-shadow: 2rpx -1px 10rpx 0px rgba(3, 0, 0, 0.2);
					background: #C0B5B6;
				}

				.input_code {
					height: 80rpx;
					// margin-top: 40rpx;
					border-radius: 10rpx;
					padding-left: 45rpx;
					padding-right: 36rpx;
					box-sizing: border-box;
					box-shadow: 2rpx -1px 10rpx 0px rgba(3, 0, 0, 0.2);
					background: #C0B5B6;
					display: flex;
					align-items: center;

					input {
						flex: 1;
						height: 80rpx;
						color: #494338;
						font-size: 28rpx;
						font-family: SimSun;
					}

					.get_code {
						color: #FC2B1D;
						font-size: 28rpx;
						font-weight: 400;
						font-family: SimSun;
					}
				}
			}

			.other {
				width: 100%;
				color: #494338;
				font-size: 24rpx;
				font-weight: 400;
				font-family: SimSun;
				display: flex;
				justify-content: space-between;
			}

			.btn {
				width: 204rpx;
				height: 74rpx;
				line-height: 74rpx;
				text-align: center;
				color: #FFFFFF;
				font-size: 30rpx;
				font-weight: 400;
				font-family: SimSun;
				background: url(#{$baseImageUrl}/common/btn.png) no-repeat;
				background-size: 100% 100%;
			}

			.explain {
				width: 100%;
				display: flex;
				justify-content: flex-start;
				align-items: center;

				image {
					width: 52rpx;
					height: 52rpx;
					margin-right: 10rpx;
				}

				.tips {
					color: #494338;
					font-size: 24rpx;
					font-weight: 400;
					font-family: SimSun;

					.desc {
						color: #21b3e9;
					}
				}
			}
		}

		// .navbar {
		// 	width: 100%;
		// 	height: $navbarHeight;
		// 	padding: 60rpx 36rpx 0;
		// 	box-sizing: border-box;
		// 	position: relative;
		// 	z-index: 99999;

		// 	.back {
		// 		width: 102rpx;
		// 		height: 52rpx;

		// 		image {
		// 			width: 100%;
		// 			height: 100%;
		// 		}
		// 	}
		// }
	}
</style>